package com.canaan.business.controller.pc.inOutStock;

import com.canaan.business.common.enums.inOutStock.StockOccupyStatusEnum;
import com.canaan.business.common.enums.inOutStock.StockOccupyTypeEnum;
import com.canaan.business.domain.inOutStock.WmsStockOccupy;
import com.canaan.business.service.inOutStock.IWmsStockOccupyService;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 库存占用Controller
 *
 * @author ruoyi
 * @date 2023-02-07
 */
@Api(tags = "库存占用")
@RestController
@RequestMapping("/inOutStock/occupy")
public class WmsStockOccupyController extends BaseController {
    @Resource
    private IWmsStockOccupyService stockOccupyService;

    /**
     * 查询库存占用列表
     */
    @ApiOperation(value = "查询库存占用列表")
    @GetMapping("/list")
    public TableDataInfo list(WmsStockOccupy stockOccupy) {
        startPage();
        List<WmsStockOccupy> list = stockOccupyService.selectWmsStockOccupyList(stockOccupy);
        return getDataTable(list);
    }

    /**
     * 导出库存占用列表
     */
    @ApiOperation(value = "导出库存占用列表")
    @Log(title = "库存占用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsStockOccupy stockOccupy) {
        List<WmsStockOccupy> list = stockOccupyService.selectWmsStockOccupyList(stockOccupy);
        for (WmsStockOccupy occupy : list) {
            occupy.setStatus(StockOccupyStatusEnum.getMessageByCode(occupy.getStatus()));
            occupy.setType(StockOccupyTypeEnum.getMessageByCode(occupy.getType()));
        }
        ExcelUtil<WmsStockOccupy> util = new ExcelUtil<WmsStockOccupy>(WmsStockOccupy.class);
        util.exportExcel(response, list, "库存占用数据");
    }

    /**
     * 获取库存占用详细信息
     */
    @ApiOperation(value = "获取库存占用详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(stockOccupyService.selectWmsStockOccupyById(id));
    }

    /**
     * 新增库存占用
     */
    @ApiOperation(value = "新增库存占用")
    @Log(title = "库存占用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsStockOccupy stockOccupy) {
        return toAjax(stockOccupyService.insertWmsStockOccupy(stockOccupy));
    }

    /**
     * 修改库存占用
     */
    @ApiOperation(value = "修改库存占用")
    @Log(title = "库存占用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsStockOccupy stockOccupy) {
        return toAjax(stockOccupyService.updateWmsStockOccupy(stockOccupy));
    }

    /**
     * 删除库存占用
     */
    @ApiOperation(value = "删除库存占用")
    @Log(title = "库存占用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(stockOccupyService.deleteWmsStockOccupyByIds(ids));
    }
}
